package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dto.OneValues;
import com.example.entity.LogInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 日志信息数据访问层接口
 * 继承自BaseMapper，提供基本的CRUD操作，并定义了特定的查询方法
 */
@Mapper
public interface LogMapper extends BaseMapper<LogInfo> {


    /**
     * 查询用户最近5次登录记录
     *
     * @param userId 用户ID
     * @return 近5次登录记录列表，按ID倒序排列
     */
    @Select("SELECT * FROM t_log_info WHERE is_login = 1 AND user_id = #{userId} AND nickname is not null ORDER BY id DESC LIMIT 5")
    List<LogInfo> selectLoginList(Integer userId);


    /**
     * 查询用户近一个月的活跃趋势统计
     * 按天统计用户操作次数，用于生成活跃度图表
     *
     * @param userId 用户ID
     * @return 每日操作次数统计列表，包含日期和对应的操作次数
     */
    @Select("SELECT " +
            "    DATE_FORMAT(create_time, '%Y-%m-%d') AS name, " +
            "    COUNT(1) AS value " +
            "FROM " +
            "    t_log_info " +
            "WHERE " +
            "  user_id = #{userId} AND create_time >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH) " +
            "GROUP BY " +
            "    name " +
            "ORDER BY " +
            "    name;")
    List<OneValues> selectActiveTrend(Integer userId);

}

